package cc.rengu.igas.cops.common.util;

import cc.rengu.igas.cops.common.dao.CopsMchntKeyInfoMapper;
import cc.rengu.igas.cops.common.dao.impl.CopsMchntKeyInfoMapperImpl;
import cc.rengu.igas.cops.common.entity.CopsMchntKeyInfo;
import cc.rengu.igas.cops.common.enums.CopsRespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.KeyInfoMapper;
import cc.rengu.oltp.service.common.dao.SecPlanInfoMapper;
import cc.rengu.oltp.service.common.dao.SrcChannelInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.KeyInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SecPlanInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SrcChannelInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.KeyInfo;
import cc.rengu.oltp.service.common.entity.SecPlanInfo;
import cc.rengu.oltp.service.common.entity.SrcChannelInfo;
import cc.rengu.oltp.utility.util.StringUtil;

public class IgasGenKeyStringUtil {


    public static String[] genKeyStringByChannelInfo(String srcMsgId, String reqId) {
        //获取密钥串
        String bankId = AppParamConstant.DEFAULT_INSTID;
        String[] keyValue = new String[2];
        try {
            SrcChannelInfoMapper srcChannelInfoMapper = new SrcChannelInfoMapperImpl();
            SecPlanInfoMapper secPlanInfoMapper = new SecPlanInfoMapperImpl();
            KeyInfoMapper keyInfoMapper = new KeyInfoMapperImpl();
            SrcChannelInfo srcChannelInfo = srcChannelInfoMapper.selectSrcChannelInfoByPrimaryKey(bankId, srcMsgId, reqId);
            if (null != srcChannelInfo) {
                if (null != srcChannelInfo.getTransChannelStatus() && AppParamConstant.YES.equals(srcChannelInfo.getTransChannelStatus())) {
                    SecPlanInfo secPlanInfo = secPlanInfoMapper.selectSecPlanInfoByPrimaryKey(bankId, srcChannelInfo.getSecPlanId(), "KE03");
                    if (null != secPlanInfo) {
                        if (null != secPlanInfo.getSecPlanStatus() && AppParamConstant.YES.equals(secPlanInfo.getSecPlanStatus())) {
                            //获取密钥值及算法
                            KeyInfo keyInfo = keyInfoMapper.selectKeyInfoByPrimaryKey(bankId + srcChannelInfo.getSecPlanId() + "KE03");
                            if (null != keyInfo && AppParamConstant.YES.equals(keyInfo.getKeyStatus())) {
                                if (StringUtil.isEmptyOrNull(keyInfo.getKeyValue())) {
                                    throw new Exception(CopsRespCodeEnum.KEY_STRING_NOT_EXSIT_ERROR.getRespCode());
                                }
                                if (StringUtil.isEmptyOrNull(keyInfo.getAlgorithmType())) {
                                    keyInfo.setAlgorithmType("SHA1");
                                }
                                keyValue[0] = keyInfo.getAlgorithmType();
                                keyValue[1] = keyInfo.getKeyValue();
                            }
                        }

                    }
                }
            }
        } catch (Exception e) {

        }
        return keyValue;
    }

    public static String[] genKeyStringByMerchantInfo(String merNo, String bankId) {
        //从商户密钥信息表获取商户密钥串
        if (StringUtil.isEmptyOrNull(bankId)) {
            bankId = AppParamConstant.DEFAULT_INSTID;
        }
        String[] keyValue = new String[2];
        CopsMchntKeyInfoMapper copsMchntKeyInfoMapper = new CopsMchntKeyInfoMapperImpl();
        CopsMchntKeyInfo copsMchntKeyInfo = new CopsMchntKeyInfo();
        try {
            copsMchntKeyInfo = copsMchntKeyInfoMapper.selectBspsMchntKeyInfoByPrimaryKey(bankId, merNo, "APPID", "00");
            if (null != copsMchntKeyInfo && AppParamConstant.YES.equals(copsMchntKeyInfo.getKeyStatus())) {
                if (StringUtil.isEmptyOrNull(copsMchntKeyInfo.getKeyValue())) {
                    throw new Exception(CopsRespCodeEnum.KEY_STRING_NOT_EXSIT_ERROR.getRespCode());
                }
                if (StringUtil.isEmptyOrNull(copsMchntKeyInfo.getAlgorithmType())) {
                    copsMchntKeyInfo.setAlgorithmType("SHA-1");
                }
                keyValue[0] = copsMchntKeyInfo.getAlgorithmType();
                keyValue[1] = copsMchntKeyInfo.getKeyValue();
            }

        } catch (Exception e) {
        }
        return keyValue;
    }

    public static String[] genKeyStringByOrgInfo(String orgNo, String bankId) {
        //从商户密钥信息表获取商户密钥串
        if (StringUtil.isEmptyOrNull(bankId)) {
            bankId = AppParamConstant.DEFAULT_INSTID;
        }
        String[] keyValue = new String[2];
        CopsMchntKeyInfoMapper copsMchntKeyInfoMapper = new CopsMchntKeyInfoMapperImpl();
        CopsMchntKeyInfo copsMchntKeyInfo;
        try {
            copsMchntKeyInfo = copsMchntKeyInfoMapper.selectBspsMchntKeyInfoByPrimaryKey(bankId, orgNo, "APPID", "00");
            if (null != copsMchntKeyInfo && AppParamConstant.YES.equals(copsMchntKeyInfo.getKeyStatus())) {
                if (StringUtil.isEmptyOrNull(copsMchntKeyInfo.getKeyValue())) {
                    throw new Exception(CopsRespCodeEnum.KEY_STRING_NOT_EXSIT_ERROR.getRespCode());
                }
                if (StringUtil.isEmptyOrNull(copsMchntKeyInfo.getAlgorithmType())) {
                    copsMchntKeyInfo.setAlgorithmType("SHA1");
                }
                keyValue[0] = copsMchntKeyInfo.getAlgorithmType();
                keyValue[1] = copsMchntKeyInfo.getKeyValue();
            }

        } catch (Exception e) {
        }
        return keyValue;
    }
}
